package org.example.java_gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

//用户在线状态管理
@Component
public class OnlineUserManager {
    //这个哈希表就用于表示当前用户在游戏大厅的在线状态
    //解决线程安全问题 这个哈希表
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();

    //这个哈希表就用来表示当前用户在房间的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    //进入游戏大厅
    public void enterGameHall(int userId,WebSocketSession webSocketSession) {
        gameHall.put(userId,webSocketSession);
    }

    //离开游戏大厅
    public void exitGameHall(int userId) {
        gameHall.remove(userId);
    }

    //查找键值对
    public WebSocketSession getFromGameHall(int userId) {
        return gameHall.get(userId);
    }

    public void enterGameRoom(int userId,WebSocketSession webSocketSession) {
        gameRoom.put(userId,webSocketSession);
    }

    public void exitGameRoom(int userId) {
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromGameRoom(int userId) {
        return gameRoom.get(userId);
    }
}
